import time
import re

from qturboframe.lib.utils.by import By
from qturboframe.lib.report.log import logger


class Frame():
    '''
    jres2.0的Frame对象

    :Attributes:
     - _driver: 控件所在的当前webdriver对象
     - timeout: 步骤操作的超时时间

    :Example:
     - 例1: Frame(driver)
    '''

    def __init__(self, driver, timeout=5):
        self.driver = driver
        self.timeout = timeout

    def swtich_to_defatul_content(self):
        '''
        jres2.0 从frame切换到主文档
        
        :Example:
         - 例: frame.swtich_to_defatul_content()
        '''
        self.driver.switch_to.default_content()

    def switch_frame(self, frameidnames=None, timeout=10):
        '''
        切换jres2.0的frame

        :Args:
         - frameidname: frame的ID或者Name；默认是没有ID和Name, 可以传入多个frameidname用/分隔
         
        :Example:
         - 例1: frame.swtich_to_defatul_content(frameidnames='4562677875')
         - 例2: frame.swtich_to_defatul_content()
        '''
        re_none = r'display:\s*none;*'
        # 先切到主页
        self.driver.switch_to.default_content()
        # 再从主页切到frame页
        if frameidnames:
            frameidnames_split = frameidnames.strip().split("/")
            for frame in frameidnames_split:
                self.driver.switch_to.frame(frame)
        else:
            timeout1 = 0
            while timeout1 < timeout:
                frame_updiv = self.driver.exist('class name', 'ui-page-tab-iframe',
                                                timeout=self.timeout)
                if frame_updiv:
                    frames = frame_updiv.find_elements_by_attribute('xpath', './iframe',
                                                                    timeout=self.timeout)
                    i = 0
                    for frame in frames:
                        if re.search(re_none, frame.get_attribute('style')):
                            i += 1
                            continue
                        else:
                            name = frame.get_attribute('name')
                            if name:
                                logger.debug('切换到frame:' + name)
                                self.driver.switch_to.frame(name)
                                break
                            id = frame.get_attribute('id')
                            if id:
                                logger.debug('切换到frame:' + id)
                                self.driver.switch_to.frame(id)
                                break
                            self.driver.switch_to.frame(i)
                            break
                    if i == len(frames):
                        timeout1 += 1
                        time.sleep(1)
                    else:
                        break
                else:
                    break
            else:
                self.driver.switch_to.default_content()

    def switch_to_displayed_frame(self, timeout=1):
        frames = self.get_displayed_frame(timeout=timeout)
        if len(frames)>0:
            self.driver.switch_to.frame(frames[0])
        else:
            logger.debug("当前页面没有iframe")

    def get_displayed_frame(self, timeout=1):
        self.driver.switch_to.default_content()
        for i in range(timeout):
            frames = self.driver.find_elements_by_attribute(By.CSS_SELECTOR,"frame,iframe", timeout=0.1)
            logger.debug("当前frames的个数为:{}".format(len(frames)))
            frame_displayed_id_lists = []
            for frame in frames:
                try:
                    if frame.is_displayed():
                        frame_displayed_id_lists.append(frame)
                except:
                    pass
            if len(frame_displayed_id_lists)<1:
                time.sleep(1)
            else:
                break

            logger.debug("当前frames的个数为:{}".format(len(frame_displayed_id_lists)))

        return frame_displayed_id_lists


